package cn.schoolwow.quickdao.module.database.ddl.query.flow.property.getEntityPropertyList;

import cn.schoolwow.quickdao.annotation.IdStrategy;
import cn.schoolwow.quickdao.domain.entity.Entity;
import cn.schoolwow.quickdao.domain.entity.Property;
import cn.schoolwow.quickdao.module.database.parent.domain.ResultSetConsumer;
import cn.schoolwow.quickdao.module.database.parent.flow.executor.ExecuteQueryConnectionFlow;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

import java.sql.ResultSet;
import java.util.List;

public class GetEntityPropertyListBySQLiteFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        setPropertyListInfo(flowContext);
        setPropertyListForeignKey(flowContext);
    }

    @Override
    public String name() {
        return "获取数据库表字段列表-SQLite数据库";
    }

    private void setPropertyListInfo(FlowContext flowContext){
        List<Entity> entityList = (List<Entity>) flowContext.checkData("entityList");
        for(Entity entity:entityList){
            flowContext.startFlow(new ExecuteQueryConnectionFlow())
                    .putTemporaryData("name", "获取表字段信息")
                    .putTemporaryData("sql", "PRAGMA table_info(`" + entity.tableName + "`);")
                    .putReturnData("resultSetConsumer",new ResultSetConsumer() {
                        @Override
                        public void consumeResultSet(ResultSet resultSet) throws Exception {
                            while (resultSet.next()) {
                                Property property = new Property();
                                property.column = resultSet.getString("name");
                                property.columnType = resultSet.getString("type");
                                property.notNull = "1".equals(resultSet.getString("notnull"));
                                if (null != resultSet.getString("dflt_value")) {
                                    property.defaultValue = resultSet.getString("dflt_value");
                                }
                                if (1 == resultSet.getInt("pk")) {
                                    property.id = true;
                                    property.strategy = IdStrategy.AutoIncrement;
                                }
                                entity.properties.add(property);
                            }
                        }
                    })
                    .execute();
        }
    }

    private void setPropertyListForeignKey(FlowContext flowContext){
        List<Entity> entityList = (List<Entity>) flowContext.checkData("entityList");

        flowContext.startFlow(new ExecuteQueryConnectionFlow())
                .putTemporaryData("name", "获取表字段外键信息")
                .putTemporaryData("sql", "select name,sql from sqlite_master where type='table';")
                .putReturnData("resultSetConsumer",new ResultSetConsumer() {
                    @Override
                    public void consumeResultSet(ResultSet resultSet) throws Exception {
                        while (resultSet.next()) {
                            for(Entity entity:entityList){
                                if(!entity.tableName.equalsIgnoreCase(resultSet.getString("name"))){
                                    continue;
                                }
                                String sql = resultSet.getString("sql");
                                for(Property property:entity.properties){
                                    if(sql.contains("foreign key(\""+property.column+"\")")){
                                        property.foreignKeyColumn = true;
                                    }
                                }
                            }
                        }
                    }
                })
                .execute();
    }
}
